<?php
/**
 * vtnsMedia provides basic Media utility methods.
 *
 * @package    vtns
 * @subpackage util
 * @author     Ky Anh Truong <kyanh.truong@vtns.vn>
 * @version    SVN: $Id: vtnsMedia.class.php 29525 2012-07-01 15:00:00Z kyanh $
 */

class vtnsMedia
{
	public static function video_image($url) {
		$image_url = parse_url($url);
		if ($image_url['host'] == 'www.youtube.com' || $image_url['host'] == 'youtube.com')
		{
			$params = explode("&", $image_url['query']);
			foreach ($params as $param) {
				if (substr($param, 0, 2) == "v=")
					return "http://img.youtube.com/vi/".substr($param, 2)."/hqdefault.jpg";
			}
		} else if($image_url['host'] == 'www.vimeo.com' || $image_url['host'] == 'vimeo.com'){
			$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/".substr($image_url['path'], 1).".php"));
			return $hash[0]["thumbnail_small"];
		}
		return false;
	}
	
	public static function video_embed($url, $w = "100%", $h = "500") {
		$image_url = parse_url($url);
		if ($image_url['host'] == 'www.youtube.com' || $image_url['host'] == 'youtube.com')
		{
			$params = explode("&", $image_url['query']);
			foreach ($params as $param) {
				if (strtolower(substr($param, 0, 2)) == "v=") 
				{
					$youtube_id = substr($param, 2);
					return '<iframe class="youtube-player" type="text/html" width="'.$w.'" height="'.$h.'" src="http://www.youtube.com/embed/'.$youtube_id.'" frameborder="0"></iframe>';
				}
			}
		}
		return false;
	}
	
	public static function nct_url($url) {
		$base_url = "http://www.nhaccuatui.com/l/";
		$nct_url = parse_url($url);
		if ($nct_url['host'] == "www.nhaccuatui.com" || $nct_url['host'] == 'nhaccuatui.com') 
		{
			$params = explode("&", $nct_url['query']);
			foreach ($params as $param) {
				if (strtolower(substr($param, 0, 2)) == "l=") 
					return $base_url.substr($param, 2);
			}
		}
		return false;
	}
	
	public static function nct_embed($url, $w = "100%", $h = "150", $auto_play = 'false') {
		$nct_url = vtnsMedia::nct_url($url);
		if ($nct_url)
		{
			return '<object width="'.$w.'" height="'.$h.'"><param name="movie" value="'.$nct_url.'" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="flashvars" value="&autostart='.$auto_play.'&shuffle=true" /><embed src="'.$nct_url.'" quality="high" wmode="transparent" type="application/x-shockwave-flash" flashvars="&autostart='.$auto_play.'&shuffle=true" width="'.$w.'" height="'.$h.'"></embed></object>';
		}
		return false;
	}
}

?>